<?php
/**
 * 翻页类
 * 
 * 使用方法:
 * header ( 'Content-Type:text/html; charset=utf-8' );
 * $qx_base_page = new Cpage ();
 * $qx_base_page ->initPageInfo ( array ('count' => '100', 'var_page' => 'page' ) );
 * echo $qx_base_page->getPageList();
 * 
 * CSS样式:
 * .Qx_first_style{}
 * .Qx_previous_style{}
 * .Qx_next_style{}
 * .Qx_last_style{}
 * 
 * @author Wu Bai Qing<wbqyyicx@gmail.com>
 * @version $Id: CPage.php 3 2011-09-03 02:47:36Z wbqyyicx $
 * @copyright Copyright (c) 2011 Qx
 */
class CPage {
	/**
	 * @var string 名称是通过GET方式传递过来的变量进行翻页,默认值'page'
	 */
	private $var_page = 'page';
	
	/**
	 * @var inter 当前页,每次翻页获取当前页数量
	 */
	private $current_page;
	
	/**
	 * @var inter 总页
	 */
	private $total_page;
	
	/**
	 * @var inter 每一页显示的数据量
	 */
	private $each_page_data = 10;
	
	/**
	 * @var string 限制翻页
	 */
	private $apply_limit = 5;
	
	/**
	 * @var string 当前翻页文件
	 */
	private $current_file;
	
	/**
	 * @param array 初始化翻页信息数组
	 */
	public $display_page_name = array ('first' => '首页', 'previous' => '上一页', 'next' => '下一页', 'last' => '末页' );
	
	/**
	 * @var mixed 显示风格
	 */
	public $display_style = 'number';
	
	/**
	 * @var inter 数字风格显示数量
	 */
	public $limit_number_style = 5;
	
	/**
	 * 初始化翻页信息
	 * 
	 * $this->initPageInfo   得到翻页变量
	 * $this->setTotalPage   设置总页
	 * $this->setFileName    设置文件名称
	 * $this->setDisplayPage 显示翻页风格
	 * $this->setCurrentPage 设置当前页
	 * $this->setApplyLimit  设置查询限制
	 * 
	 * @param arrat $page_info
	 */	
	public function initPageInfo($page_info)
	{
		$this->setPageVar ( $page_info ['var_page'] );
		$this->setTotalPage ( $page_info ['count'] );
		$this->setFileName ();
		$this->setDisplayPage ( 'select' );
		$this->setCurrentPage ();
		$this->setApplyLimit ();
	}
	
	/**
	 * @return string 翻页显示的结果
	 */
	public function getPageList()
	{
		return $this->getFirstLink ().$this->getPreviousLink().$this->getNumberStyle().$this->getNextLink().$this->getLastLink();
	}
	
	/**
	 * @return string 返回首页链接,不符合链接条件返回文本
	 */
	public function getFirstLink()
	{
		if ($this->current_page > 1)
		{
			$first = "<span class='Qx_first_style'><a href='{$this->current_file}1'>{$this->display_page_name['first']}</a></span>";
		}
		else
		{
			$first = $this->display_page_name ['first'];
		}
		return $first;
	}
	
	/**
	 * @return string 返回上一页链接,不符合链接条件返回文本
	 */
	public function getPreviousLink()
	{
	
		if($this->current_page <= 1)
		{
			$previous = $this->display_page_name['previous'];
		}
		else
		{
			$previous = "<span class='Qx_previous_style'><a href='{$this->current_file}".($this->current_page - 1)."'>{$this->display_page_name['previous']}</a></span>";
		}
		return $previous;
	}
	
	/**
	 * @return string 返回下一页链接,不符合链接条件返回文本
	 */
	public function getNextLink()
	{
		if($this->current_page < $this->total_page)
		{
			$next = "<span class='Qx_next_style'><a href='{$this->current_file}".($this->current_page + 1)."'>{$this->display_page_name['next']}</a></span>";
		}
		else 
		{
			$next = $this->display_page_name['next'];
		}
		return $next;
	}
	
	/**
	 * @return string 返回尾页链接,不符合链接条件返回文本
	 */
	public function getLastLink()
	{
		if($this->current_page < $this->total_page)
		{
			$last_page = "<span class='Qx_last_style'><a href='{$this->current_file}{$this->total_page}'>{$this->display_page_name['last']}</a></span>";
		}
		else 
		{
			$last_page = $this->display_page_name['last'];
		}
		return $last_page;
	}
	
	/**
	 * 获取当前页数
	 */
	public function setCurrentPage()
	{
		$this->current_page = ! isset ( $_GET [$this->var_page] ) || empty ( $_GET [$this->var_page] ) ? 1 : $_GET [$this->var_page];
		$this->current_page = ($this->current_page < 1) ? 1 : $this->current_page;
		$this->current_page = ($this->current_page >= $this->total_page) ? $this->total_page : $this->current_page;
	}
	
	/**
	 * 设置查询限制
	 */
	public function setApplyLimit()
	{
		$this->apply_limit = ($this->current_page - 1) * $this->each_page_data;
		$this->apply_limit = ($this->apply_limit < 0) ? 0 : $this->apply_limit;
		$this->apply_limit .= ',' . $this->each_page_data;
	}
	
	/**
	 * 设置每页显示的数量
	 * 
	 * @param inter $each_page_data
	 */
	public function setEachPage($each_page_data)
	{
		$this->each_page_data = $each_page_data;
	}
	
	/**
	 * 设置get变量名称
	 * 
	 * @param string $var_name
	 */
	public function setPageVar($var_name) {
		$this->page_var = $var_name;
	}
	
	/**
	 * 显示翻页风格
	 * 
	 * @param mixed $style 风格可能是数组或字符串,如果是数组显示不同风格
	 * @author Wu Bai Qing<wbqyyicx@gmail.com>
	 */
	public function setDisplayPage($style)
	{
		$this->display_style = $style;
	}
	
	/**
	 * 设置翻页名称
	 */
	public function setFileName()
	{
		$url = $_SERVER ['REQUEST_URI'];
		$parse_url = parse_url($url);
		$path = str_replace ('/','',$parse_url['path']);
		
		$query = '';
		if(isset($parse_url['query']))
		{
			if(strpos($parse_url['query'],"?{$this->var_page}"))
			{
				$query = "?{$this->var_page}=";
			}
			else if(strpos($parse_url['query'], "&{$this->var_page}"))
			{
				$parse_url['query'] = preg_replace('/(^|&)page=(\w)/','',$parse_url['query']);
				if($parse_url['query'])
				{
					$query = '?'.$parse_url['query']."&{$this->var_page}=";
				}
			}
			else 
			{
				$query = "?{$this->var_page}=";
			}
		}
		else 
		{
			$query = "?{$this->var_page}=";
		}
		
		$this->current_file = $path.$query;
	}
	
	/**
	 * 设置总页数量
	 * 
	 * @param inter $total_page 总页数量
	 */
	private function setTotalPage($total_page)
	{
		$this->total_page = ceil ( $total_page / $this->each_page_data );
	}
	
	/**
	 * 数字风格显示 
	 */
	public function getNumberStyle()
	{
		$start_page = $offset_page = '';
		
		if($this->total_page < $this->limit_number_style)
		{
			$start_page = 1;
			$last_page = $this->total_page;
		}
		else
		{
			$offset_page = floor($this->limit_number_style / 2);
			
			if($this->current_page - $offset_page <= 1)
			{
				$start_page = 1;
			}
			else if(($this->current_page + $offset_page) > $this->total_page)
			{
				$start_page = ($this->total_page - $this->limit_number_style) + 1;
			}
			else 
			{
				$start_page = $this->current_page - $offset_page;
			}
			
			$last_page = ($start_page + $this->limit_number_style) - 1;
		}
		
		$numer_string = '';
		for($i=$start_page;$i<=$last_page;$i++)
		{
			if($this->current_page == $i)
			{
				$numer_string.=$i;
			}
			else 
			{
				$numer_string.="<a href='{$this->current_file}".$i."'>{$i}</a>";
			}
		}
		return $numer_string;
	}
}




?>
